package org.rcy.framework.thrift.serializer;

import net.qiyuesuo.rpc.thrift.metadata.FieldValueMetaData;
import org.apache.thrift.TBaseHelper;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;

import java.nio.ByteBuffer;

/**
 * Binary类型数据序列化
 */
public class BinarySerializer implements Serializer {

	@Override
	public Object read(TProtocol iprot, FieldValueMetaData valueMetaData) throws TException {
		ByteBuffer buffer = iprot.readBinary();
		return TBaseHelper.rightSize(buffer);
	}

	@Override
	public void write(TProtocol oprot, FieldValueMetaData valueMetaData, Object value) throws TException {
		if (value == null) {
			return;
		}
		ByteBuffer buf = (ByteBuffer) value;
		oprot.writeBinary(buf);
	}
}
